//
//  UIStandardSTControlView.m
//  vhECGTools
//
//  Created by jia yu on 2022/7/12.
//

#import "UIStandardSTControlView.h"
#import <vhECGTools/vhECGTools.h>
@interface UIStandardSTControlView(){
    float screenScale;
    float space;
    NSMutableArray *leadSelectItems;
}

@end

@implementation UIStandardSTControlView
@synthesize style;
@synthesize checkButton;

@synthesize textLabel;
@synthesize leadSelectButton;

@synthesize stNameLabel;
@synthesize stInputField;
@synthesize unitSTLabel;

@synthesize slopeNameLabel;
@synthesize slopeInputField;
@synthesize unitSlopeLabel;

@synthesize deltaSTNameLabel,deltaSTInputField,unitDeltaSTLabel;

-(void)setStyle:(UIStandardSTControlView_Style)_style{
    style = _style;
    float currentX = CGRectGetMaxX(leadSelectButton.frame) + 4 * space;
    switch (style) {
        case UIStandardSTControlView_Style_Default:
            stNameLabel.alpha = 1.f;
            [stNameLabel setFrame:CGRectMake(currentX, stNameLabel.frame.origin.y, stNameLabel.frame.size.width, stNameLabel.frame.size.height)];
            currentX = CGRectGetMaxX(stNameLabel.frame) + space;
            stInputField.alpha = 1.f;
            [stInputField setFrame:CGRectMake(currentX, stInputField.frame.origin.y, stInputField.frame.size.width, stInputField.frame.size.height)];
            currentX = CGRectGetMaxX(stInputField.frame) + space;
            unitSTLabel.alpha = 1.f;
            [unitSTLabel setFrame:CGRectMake(currentX, unitSTLabel.frame.origin.y, unitSTLabel.frame.size.width, unitSTLabel.frame.size.height)];
            currentX = CGRectGetMaxX(unitSTLabel.frame) + 4 * space;
            slopeNameLabel.alpha = 1.f;
            [slopeNameLabel setFrame:CGRectMake(currentX, slopeNameLabel.frame.origin.y, slopeNameLabel.frame.size.width, slopeNameLabel.frame.size.height)];
            currentX = CGRectGetMaxX(slopeNameLabel.frame) + space;
            slopeInputField.alpha = 1.f;
            [slopeInputField setFrame:CGRectMake(currentX, slopeInputField.frame.origin.y, slopeInputField.frame.size.width, slopeInputField.frame.size.height)];
            currentX = CGRectGetMaxX(slopeInputField.frame) + space;
            unitSlopeLabel.alpha = 1.f;
            [unitSlopeLabel setFrame:CGRectMake(currentX, unitSlopeLabel.frame.origin.y, unitSlopeLabel.frame.size.width, unitSlopeLabel.frame.size.height)];
            deltaSTNameLabel.alpha = 1.f;
            [deltaSTNameLabel setFrame:CGRectMake(currentX, deltaSTNameLabel.frame.origin.y, deltaSTNameLabel.frame.size.width, deltaSTNameLabel.frame.size.height)];
            currentX = CGRectGetMaxX(deltaSTNameLabel.frame) + space;
            deltaSTInputField.alpha = 1.f;
            [deltaSTInputField setFrame:CGRectMake(currentX, deltaSTInputField.frame.origin.y, deltaSTInputField.frame.size.width, deltaSTInputField.frame.size.height)];
            currentX = CGRectGetMaxX(deltaSTInputField.frame) + space;
            unitDeltaSTLabel.alpha = 1.f;
            [unitDeltaSTLabel setFrame:CGRectMake(currentX, unitDeltaSTLabel.frame.origin.y, unitDeltaSTLabel.frame.size.width, unitDeltaSTLabel.frame.size.height)];
            break;
        case UIStandardSTControlView_Style_ST:
            stNameLabel.alpha = 0.f;
            stInputField.alpha = 1.f;
            [stInputField setFrame:CGRectMake(currentX, stInputField.frame.origin.y, stInputField.frame.size.width, stInputField.frame.size.height)];
            currentX = CGRectGetMaxX(stInputField.frame) + space;
            unitSTLabel.alpha = 1.f;
            [unitSTLabel setFrame:CGRectMake(currentX, unitSTLabel.frame.origin.y, unitSTLabel.frame.size.width, unitSTLabel.frame.size.height)];
            currentX = CGRectGetMaxX(unitSTLabel.frame) + 6 * space;
            slopeNameLabel.alpha = 0.f;
            slopeInputField.alpha = 1.f;
            [slopeInputField setFrame:CGRectMake(currentX, slopeInputField.frame.origin.y, slopeInputField.frame.size.width, slopeInputField.frame.size.height)];
            currentX = CGRectGetMaxX(slopeInputField.frame) + space;
            unitSlopeLabel.alpha = 1.f;
            [unitSlopeLabel setFrame:CGRectMake(currentX, unitSlopeLabel.frame.origin.y, unitSlopeLabel.frame.size.width, unitSlopeLabel.frame.size.height)];
            deltaSTNameLabel.alpha = 0.f;
            deltaSTInputField.alpha = 0.f;
            unitDeltaSTLabel.alpha = 0.f;
            break;
        case UIStandardSTControlView_Style_Slope:
            slopeNameLabel.alpha = 0.f;
            slopeInputField.alpha = 1.f;
            [slopeInputField setFrame:CGRectMake(currentX, slopeInputField.frame.origin.y, slopeInputField.frame.size.width, slopeInputField.frame.size.height)];
            currentX = CGRectGetMaxX(slopeInputField.frame) + space;
            unitSlopeLabel.alpha = 1.f;
            [unitSlopeLabel setFrame:CGRectMake(currentX, unitSlopeLabel.frame.origin.y, unitSlopeLabel.frame.size.width, unitSlopeLabel.frame.size.height)];
            currentX = CGRectGetMaxX(unitSlopeLabel.frame) + 3.4 * space;
            stNameLabel.alpha = 1.f;
            [stNameLabel setFrame:CGRectMake(currentX, stNameLabel.frame.origin.y, stNameLabel.frame.size.width, stNameLabel.frame.size.height)];
            currentX = CGRectGetMaxX(stNameLabel.frame) + space;
            stInputField.alpha = 1.f;
            [stInputField setFrame:CGRectMake(currentX, stInputField.frame.origin.y, stInputField.frame.size.width, stInputField.frame.size.height)];
            currentX = CGRectGetMaxX(stInputField.frame) + space;
            unitSTLabel.alpha = 1.f;
            [unitSTLabel setFrame:CGRectMake(currentX, unitSTLabel.frame.origin.y, unitSTLabel.frame.size.width, unitSTLabel.frame.size.height)];
            currentX = CGRectGetMaxX(unitSTLabel.frame) + 4 * space;
            deltaSTNameLabel.alpha = 1.f;
            [deltaSTNameLabel setFrame:CGRectMake(currentX, deltaSTNameLabel.frame.origin.y, deltaSTNameLabel.frame.size.width, deltaSTNameLabel.frame.size.height)];
            currentX = CGRectGetMaxX(deltaSTNameLabel.frame) + space;
            deltaSTInputField.alpha = 1.f;
            [deltaSTInputField setFrame:CGRectMake(currentX, deltaSTInputField.frame.origin.y, deltaSTInputField.frame.size.width, deltaSTInputField.frame.size.height)];
            currentX = CGRectGetMaxX(deltaSTInputField.frame) + space;
            unitDeltaSTLabel.alpha = 1.f;
            [unitDeltaSTLabel setFrame:CGRectMake(currentX, unitDeltaSTLabel.frame.origin.y, unitDeltaSTLabel.frame.size.width, unitDeltaSTLabel.frame.size.height)];
            break;
        case UIStandardSTControlView_Style_DeltaST:
            stNameLabel.alpha = 0.f;
            stInputField.alpha = 1.f;
            [stInputField setFrame:CGRectMake(currentX, stInputField.frame.origin.y, stInputField.frame.size.width, stInputField.frame.size.height)];
            currentX = CGRectGetMaxX(stInputField.frame) + space;
            unitSTLabel.alpha = 1.f;
            [unitSTLabel setFrame:CGRectMake(currentX, unitSTLabel.frame.origin.y, unitSTLabel.frame.size.width, unitSTLabel.frame.size.height)];
            currentX = CGRectGetMaxX(unitSTLabel.frame) + 6 * space;
            slopeNameLabel.alpha = 0.f;
            slopeInputField.alpha = 1.f;
            [slopeInputField setFrame:CGRectMake(currentX, slopeInputField.frame.origin.y, slopeInputField.frame.size.width, slopeInputField.frame.size.height)];
            currentX = CGRectGetMaxX(slopeInputField.frame) + space;
            unitSlopeLabel.alpha = 1.f;
            [unitSlopeLabel setFrame:CGRectMake(currentX, unitSlopeLabel.frame.origin.y, unitSlopeLabel.frame.size.width, unitSlopeLabel.frame.size.height)];
            currentX = CGRectGetMaxX(unitSlopeLabel.frame) + 4 * space;
            deltaSTNameLabel.alpha = 1.f;
            [deltaSTNameLabel setFrame:CGRectMake(currentX, deltaSTNameLabel.frame.origin.y, deltaSTNameLabel.frame.size.width, deltaSTNameLabel.frame.size.height)];
            currentX = CGRectGetMaxX(deltaSTNameLabel.frame) + space;
            deltaSTInputField.alpha = 1.f;
            [deltaSTInputField setFrame:CGRectMake(currentX, deltaSTInputField.frame.origin.y, deltaSTInputField.frame.size.width, deltaSTInputField.frame.size.height)];
            currentX = CGRectGetMaxX(deltaSTInputField.frame) + space;
            unitDeltaSTLabel.alpha = 1.f;
            [unitDeltaSTLabel setFrame:CGRectMake(currentX, unitDeltaSTLabel.frame.origin.y, unitDeltaSTLabel.frame.size.width, unitDeltaSTLabel.frame.size.height)];
            break;
        default:
            break;
    }
    return;
}

-(id)initWithFrame:(CGRect)frame scale:(float)scale{
    self = [super initWithFrame:frame];
    if (self){
        screenScale = scale;
        space = 12.f * scale;
        UIStandardSTControlView *__strong strongSelf = self;
        float currentX = 0.f;
        checkButton = [[UIStandardCheckBoxButton alloc] initWithFrame:CGRectMake(-20.f * screenScale, -25.f * screenScale, 90.f * screenScale, 90.f * screenScale) scale:screenScale];
        checkButton.layer.masksToBounds = YES;
        [self addSubview:checkButton];
        
        textLabel = [[UIStandardLabel alloc] initWithFrame:CGRectMake(60.f * screenScale, 0.f, 650.f * screenScale, 40.f * screenScale)];
        textLabel.font = [UIFont fontWithName:@"PingFangSC-Thin" size:34.f * screenScale];
        textLabel.textColor = [vhECGTools colorFromHEXString:@"606972"];
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.text = @"Max ST";
        [self addSubview:textLabel];
        
        leadSelectButton = [[UIStandardSelectButton alloc] initWithFrame:CGRectMake(750.f * screenScale, 2.f * screenScale, 160.f*screenScale, 56.f * screenScale) scale:screenScale];
        leadSelectButton.style = UIStandardSelectButton_Style_Board;
        leadSelectItems = [[NSMutableArray alloc] init];
        for (int i=0;i<12;i++){
            NSItemObject *item = [[NSItemObject alloc] init];
            item.inner = i;
            item.str = [vhECGTools leadName:i];
            [leadSelectItems addObject:item];
        }
        leadSelectButton.availableItems = leadSelectItems;
        leadSelectButton.changedHandler = ^(NSString * _Nonnull targetString) {
            for (NSItemObject *leadObject in strongSelf->leadSelectItems){
                if ([leadObject.str isEqualToString:targetString]){
                    strongSelf->leadSelectButton.inner = leadObject.inner;
                    break;
                }
            }
        };
        [self addSubview:leadSelectButton];
        
        currentX = CGRectGetMaxX(leadSelectButton.frame) + 40.f * screenScale;
        stNameLabel = [[UIStandardLabel alloc] initWithFrame:CGRectMake(currentX, 0.f, 64.f * screenScale, frame.size.height) scale:screenScale];
        stNameLabel.text = @"ST=";
        [self addSubview:stNameLabel];
        
        currentX = CGRectGetMaxX(stNameLabel.frame) + 40.f * screenScale;
        stInputField = [[UIStandardInputTextField alloc] initWithFrame:CGRectMake(currentX, 0.f, 160.f*screenScale, 60.f * screenScale) scale:screenScale];
        stInputField.textAlignment = NSTextAlignmentLeft;
        stInputField.style = UIStandardInputTextField_Style_WithBoard;
//        stInputField.regex = @"^(0.[0-9][0-9])$";
        [self addSubview:stInputField];
        
        currentX = CGRectGetMaxX(stInputField.frame) + 12.f * screenScale;
        unitSTLabel = [[UIStandardLabel alloc] initWithFrame:CGRectMake(currentX, 0.f, 60.f*screenScale, 60.f * screenScale) scale:screenScale];
        unitSTLabel.text = @"mm";
        [self addSubview:unitSTLabel];
        
        currentX = CGRectGetMaxX(unitSTLabel.frame) + 12.f * screenScale;
        slopeNameLabel = [[UIStandardLabel alloc] initWithFrame:CGRectMake(currentX, 0.f, 120.f * screenScale, frame.size.height) scale:screenScale];
        slopeNameLabel.text = @"Slope=";
        [self addSubview:slopeNameLabel];
        
        currentX = CGRectGetMaxX(slopeNameLabel.frame) + space;
        slopeInputField = [[UIStandardInputTextField alloc] initWithFrame:CGRectMake(currentX, 0.f, 160.f*screenScale, 60.f * screenScale) scale:screenScale];
        slopeInputField.textAlignment = NSTextAlignmentLeft;
        slopeInputField.style = UIStandardInputTextField_Style_WithBoard;
//        slopeInputField.regex = @"^(0.[0-9][0-9][0-9])$";
        [self addSubview:slopeInputField];
        
        currentX = CGRectGetMaxX(slopeInputField.frame) + space;
        unitSlopeLabel = [[UIStandardLabel alloc] initWithFrame:CGRectMake(currentX, 0.f, 90.f*screenScale, 60.f * screenScale) scale:screenScale];
        unitSlopeLabel.text = @"mV/s";
        [self addSubview:unitSlopeLabel];
        
        currentX = CGRectGetMaxX(unitSlopeLabel.frame) + space;
        deltaSTNameLabel = [[UIStandardLabel alloc] initWithFrame:CGRectMake(currentX, 0.f, 140.f * screenScale, frame.size.height) scale:screenScale];
        deltaSTNameLabel.text = @"DeltaST=";
        [self addSubview:deltaSTNameLabel];
        
        currentX = CGRectGetMaxX(deltaSTNameLabel.frame) + space;
        deltaSTInputField = [[UIStandardInputTextField alloc] initWithFrame:CGRectMake(currentX, 0.f, 160.f*screenScale, 60.f * screenScale) scale:screenScale];
        deltaSTInputField.textAlignment = NSTextAlignmentLeft;
        deltaSTInputField.style = UIStandardInputTextField_Style_WithBoard;
//        deltaSTInputField.regex = @"^(0.[0-9][0-9][0-9])$";
        [self addSubview:deltaSTInputField];
        
        currentX = CGRectGetMaxX(deltaSTInputField.frame) + space;
        unitDeltaSTLabel = [[UIStandardLabel alloc] initWithFrame:CGRectMake(currentX, 0.f, 130.f*screenScale, 60.f * screenScale) scale:screenScale];
        unitDeltaSTLabel.text = @"mm";
        [self addSubview:unitDeltaSTLabel];
    }
    return self;
}
@end
